/*
 * @# RedisSupport.java 2011-12-23 上午10:10:25
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.common.redis;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import com.ianswer.common.util.spring.SpringContextHolder;

/**
 * redis nosql处理类的父类, 处理redis客户端的获取.
 * 
 * @author alvin hwang
 */
public abstract class RedisSupport {
	
	private static final Long SETTED_IF_EXISTS = 1L;
	private static final Long CREATE_NEW = 0L;
	
	private static final ShardedJedisPool JEDIS_POOL;
	static {
		JEDIS_POOL = (ShardedJedisPool) SpringContextHolder.getBean("shardedJedisPool");
	}
	
	protected static ShardedJedis redis() {
		return JEDIS_POOL.getResource();
	}
	
	protected static boolean settedIfExists(Long value) {
		return value.longValue() == SETTED_IF_EXISTS.longValue();
	}
	
	protected static boolean createNew(Long value) {
		return value.longValue() == CREATE_NEW.longValue();
	}
	
	protected static boolean delSuccess(Long value) {
		return value.longValue() > 0;
	}
	
	/**
	 * 获取String型的值, 若值不存在或为空串, 则返回默认值: 空串.
	 */
	protected static String getString(Map<String, String> cache, String field) {
		return getString(cache, field, StringUtils.EMPTY);
	}
	
	/**
	 * 获取String型的值, 若值不存在或为空串, 则返回null.
	 */
	protected static String getStringToNull(Map<String, String> cache, String field) {
		return getString(cache, field, null);
	}
	
	/**
	 * 获取String型的值, 若值不存在或为空串, 则返回指定的默认值: defaultString.
	 */
	protected static String getString(Map<String, String> cache, String field, String defaultString) {
		String value = cache.get(field);
		if (StringUtils.isEmpty(value)) {
			return defaultString;
		}
		return value;
	}
	
	/**
	 * 获取String型的值, 若值不存在或为空串, 则返回默认值: 空串.
	 */
	protected static String getString(String key, String field) {
		return getString(key, field, StringUtils.EMPTY);
	}
	
	/**
	 * 获取String型的值, 若值不存在或为空串, 则返回null.
	 */
	protected static String getStringToNull(String key, String field) {
		return getString(key, field, null);
	}
	
	/**
	 * 获取String型的值, 若值不存在或为空串, 则返回指定的默认值: defaultString.
	 */
	protected static String getString(String key, String field, String defaultString) {
		String value = redis().hget(key, field);
		if (StringUtils.isEmpty(value)) {
			return defaultString;
		}
		return value;
	}
	
	/**
	 * 获取Long型的值, 若值不存在或为空串, 则返回默认值: 0.
	 */
	protected static Long getLong(Map<String, String> cache, String field) {
		return getLong(cache, field, 0L);
	}
	
	/**
	 * 获取Long型的值, 若值不存在或为空串, 则返回null.
	 */
	protected static Long getLongToNull(Map<String, String> cache, String field) {
		return getLong(cache, field, null);
	}
	
	/**
	 * 获取Long型的值, 若值不存在或为空串, 则返回指定的默认值: defaultLong.
	 */
	protected static Long getLong(Map<String, String> cache, String field, Long defaultLong) {
		String value = cache.get(field);
		if (StringUtils.isBlank(value)) {
			return defaultLong;
		}
		return Long.valueOf(value);
	}
	
	/**
	 * 获取Long型的值, 若值不存在或为空串, 则返回默认值: 0.
	 */
	protected static Long getLong(String key, String field) {
		return getLong(key, field, 0L);
	}
	
	/**
	 * 获取Long型的值, 若值不存在或为空串, 则返回null.
	 */
	protected static Long getLongToNull(String key, String field) {
		return getLong(key, field, null);
	}
	
	/**
	 * 获取Long型的值, 若值不存在或为空串, 则返回指定的默认值: defaultLong.
	 */
	protected static Long getLong(String key, String field, Long defaultLong) {
		String value = redis().hget(key, field);
		if (StringUtils.isBlank(value)) {
			return defaultLong;
		}
		return Long.valueOf(value);
	}
	
	/**
	 * 获取int型的值, 若值不存在或为空串, 则返回默认值: 0.
	 */
	protected static int getInt(Map<String, String> cache, String field) {
		return getInt(cache, field, 0);
	}
	
	/**
	 * 获取int型的值, 若值不存在或为空串, 则返回指定的默认值: defaultInt.
	 */
	protected static int getInt(Map<String, String> cache, String field, int defaultInt) {
		String value = cache.get(field);
		if (StringUtils.isBlank(value)) {
			return defaultInt;
		}
		return Integer.parseInt(value);
	}
	
	/**
	 * 获取int型的值, 若值不存在或为空串, 则返回默认值: 0.
	 */
	protected static int getInt(String key, String field) {
		return getInt(key, field, 0);
	}
	
	/**
	 * 获取int型的值, 若值不存在或为空串, 则返回指定的默认值: defaultInt.
	 */
	protected static int getInt(String key, String field, int defaultInt) {
		String value = redis().hget(key, field);
		if (StringUtils.isBlank(value)) {
			return defaultInt;
		}
		return Integer.parseInt(value);
	}
}
